/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-05 Marco Riccardi - initial implementation
 *	2011-03-20 Marco Riccardi - added method toXMLString() 
 *
 */
package org.mizartools.utility.xml;

import java.util.LinkedList;
import java.util.List;

import org.mizartools.utility.Html;

public final class XMLElement {

	private boolean isEmpty = false;
	private boolean isEndElement = false;
	private final String name;
	private int level = 0;
	private String characters = null;
	private List<XMLAttribute> xmlAttributeList = null;
	
	public XMLElement(String name){
		this.name = name;
	}
	
	public void setEmpty(boolean isEmpty) {
		this.isEmpty = isEmpty;
	}

	public boolean isEmpty() {
		return isEmpty;
	}

	public String getName() {
		return name;
	}

	public List<XMLAttribute> getXMLAttributeList() {
		if (xmlAttributeList == null){
			xmlAttributeList = new LinkedList<XMLAttribute>();
		}
		return xmlAttributeList;
	}

	public void setEndElement(boolean isEndElement) {
		this.isEndElement = isEndElement;
	}

	public boolean isEndElement() {
		return isEndElement;
	}
	
	public String toXMLString() {
		StringBuilder sb = new StringBuilder("");
		for (int i=0; i<level; i++){
			sb.append(" ");
		}
		sb.append("<");
		if (isEndElement()){
			sb.append("/");
		}
		sb.append(Html.changeAllSpecialChars(name));
		if (xmlAttributeList != null) {
			for(XMLAttribute xmlAttribute: xmlAttributeList){
				sb.append(" ");
				sb.append(xmlAttribute.toXMLString());
			}
		}
		if (isEmpty()){
			sb.append("/");
		}
		sb.append(">");
		if (characters != null){
			sb.append(characters);
		}
		return sb.toString();
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder("");
		for (int i=0; i<level; i++){
			sb.append(" ");
		}
		sb.append("<");
		if (isEndElement()){
			sb.append("/");
		}
		sb.append(name);
		if (xmlAttributeList != null) {
			for(XMLAttribute xmlAttribute: xmlAttributeList){
				sb.append(" ");
				sb.append(xmlAttribute.toString());
			}
		}
		if (isEmpty()){
			sb.append("/");
		}
		sb.append(">");
		if (characters != null){
			sb.append(characters);
		}
		return sb.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((characters == null) ? 0 : characters.hashCode());
		result = prime * result + (isEmpty ? 1231 : 1237);
		result = prime * result + (isEndElement ? 1231 : 1237);
		result = prime * result + level;
		result = prime
				* result
				+ ((xmlAttributeList == null) ? 0 : xmlAttributeList.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		XMLElement other = (XMLElement) obj;
		if (characters == null) {
			if (other.characters != null)
				return false;
		} else if (!characters.equals(other.characters))
			return false;
		if (isEmpty != other.isEmpty)
			return false;
		if (isEndElement != other.isEndElement)
			return false;
		if (level != other.level)
			return false;
		if (xmlAttributeList == null) {
			if (other.xmlAttributeList != null)
				return false;
		} else if (!xmlAttributeList.equals(other.xmlAttributeList))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public int getLevel() {
		return level;
	}

	public void setCharacters(String characters) {
		this.characters = characters;
	}

	public String getCharacters() {
		return characters;
	}

}
